package com.woan.api.services;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.woan.api.util.DateJsonValueProcessor;
import com.woan.api.util.PushAsync;
import com.woan.api.vo.VmessageList;
import com.woan.api.idao.SysAdmininfoIDAO;
import com.woan.api.idao.SysMessageIDAO;
import com.woan.api.model.SysAdmininfo;
import com.woan.api.model.SysMessage;


import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;



public class MessageClientServices {
	private SysAdmininfoIDAO sysadmininfoiDAO;
	private SysMessageIDAO sysmessageiDAO;
	public SysAdmininfoIDAO getSysadmininfoiDAO() {
		return sysadmininfoiDAO;
	}
	public void setSysadmininfoiDAO(SysAdmininfoIDAO sysadmininfoiDAO) {
		this.sysadmininfoiDAO = sysadmininfoiDAO;
	}


	public SysMessageIDAO getSysmessageiDAO() {
		return sysmessageiDAO;
	}
	public void setSysmessageiDAO(SysMessageIDAO sysmessageiDAO) {
		this.sysmessageiDAO = sysmessageiDAO;
	}
	/*
	 * 获取本组历史消息
	 */
	public List<VmessageList> listHistoryMessage(String rid,Integer beginIndex,Integer endIndex){
		
		List<VmessageList> message = null;
		message = sysmessageiDAO.FindHistoryMessageByGroup(rid, beginIndex, endIndex);
		return message;
	}
	public JSONObject sendMessage(String groupname,String username,String rid,String uid,SysMessage sysmessage){
		boolean flag = false;
		sysmessage.setRid(rid);
		sysmessage.setSuid(uid);
		flag = sysmessageiDAO.SaveMessage(sysmessage);
		JSONObject json=new JSONObject();
		if(flag)
		{
			json.accumulate("results", 0);
			json.accumulate("errMsg", "发送成功");
			List Androidpushlist = sysmessageiDAO.Getpushlist(rid, username, "android");
			List Iospushlist = sysmessageiDAO.Getpushlist(rid, username, "ios");
			if(Androidpushlist.size()>0)
			{
				if(sysmessage.getMsgType().equals("1"))//文字消息
				{
					PushAsync sendAsync = new PushAsync(groupname,username,sysmessage.getMsgText(),Androidpushlist,"android","groupmestext");
					sendAsync.send();
				}
				if(sysmessage.getMsgType().equals("0"))//语音消息
				{
					PushAsync sendAsync = new PushAsync(groupname,username,sysmessage.getMsgText(),Androidpushlist,"android","voicemsg");
					sendAsync.send();
				}
			}
			if(Iospushlist.size()>0)
			{
				if(sysmessage.getMsgType().equals("1"))//文字消息
				{
					PushAsync sendAsync = new PushAsync(groupname,username,sysmessage.getMsgText(),Iospushlist,"ios","groupmestext");
					sendAsync.send();
				}
				if(sysmessage.getMsgType().equals("0"))//语音消息
				{
					PushAsync sendAsync = new PushAsync(groupname,username,sysmessage.getMsgText(),Iospushlist,"ios","voicemsg");
					sendAsync.send();
				}
			}
		}
		else
		{
			json.accumulate("results", 1);
			json.accumulate("errMsg", "发送失败");
		}
		return json;
	}
}
